using Maticsoft.DBUtility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace uTrade.Data
{
    public class StockInfoManager
    {
        public StockInfoManager()
        { }

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public static int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("Symbol", "tbl_Stock_Info");
        }

        /// <summary>
        /// 清空
        /// </summary>
        public static int Clear()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbl_Stock_Info ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            return rows;
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(string symbol)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbl_Stock_Info");
            strSql.Append(" where Symbol=@symbol");
            SqlParameter[] parameters = {
                    new SqlParameter("@symbol", SqlDbType.VarChar ,10)
            };
            parameters[0].Value = symbol;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static int Add(StockInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbl_Stock_Info(");
            strSql.Append("Symbol, Type, OneHand, Name, PointIndex, YestClose, Unknow1, Unknow2, Unknow3, Favorite)");
            strSql.Append(" values (");
            strSql.Append("@Symbol, @Type, @OneHand, @Name, @PointIndex, @YestClose, @Unknow1, @Unknow2, @Unknow3, @Favorite)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@Symbol", SqlDbType.VarChar ,10),
new SqlParameter("@Type", SqlDbType.NVarChar ,50),
new SqlParameter("@OneHand", SqlDbType.NVarChar ,50),
new SqlParameter("@Name", SqlDbType.NVarChar ,50),
new SqlParameter("@PointIndex", SqlDbType.NVarChar ,50),
new SqlParameter("@YestClose", SqlDbType.Decimal ,9),
new SqlParameter("@Unknow1", SqlDbType.NVarChar ,50),
new SqlParameter("@Unknow2", SqlDbType.NVarChar ,50),
new SqlParameter("@Unknow3", SqlDbType.NVarChar ,50),
new SqlParameter("@Favorite", SqlDbType.Int)};
            parameters[0].Value = model.Symbol;
            parameters[1].Value = model.Type;
            parameters[2].Value = model.OneHand;
            parameters[3].Value = model.Name;
            parameters[4].Value = model.PointIndex;
            parameters[5].Value = model.YestClose;
            parameters[6].Value = model.Unknow1;
            parameters[7].Value = model.Unknow2;
            parameters[8].Value = model.Unknow3;
            parameters[9].Value = model.Favorite;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static int Update(StockInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbl_Stock_Info set ");
            strSql.Append("Type = @Type,");
            strSql.Append("OneHand = @OneHand,");
            strSql.Append("Name = @Name,");
            strSql.Append("PointIndex = @PointIndex,");
            strSql.Append("YestClose = @YestClose,");
            strSql.Append("Unknow1 = @Unknow1,");
            strSql.Append("Unknow2 = @Unknow2,");
            strSql.Append("Unknow3 = @Unknow3,");
            strSql.Append("Favorite = @Favorite,");

            strSql.Append(" where Symbol=@symbol");
            SqlParameter[] parameters = {
                    new SqlParameter("@Symbol", SqlDbType.VarChar ,10),
new SqlParameter("@Type", SqlDbType.NVarChar ,50),
new SqlParameter("@OneHand", SqlDbType.NVarChar ,50),
new SqlParameter("@Name", SqlDbType.NVarChar ,50),
new SqlParameter("@PointIndex", SqlDbType.NVarChar ,50),
new SqlParameter("@YestClose", SqlDbType.Decimal ,9),
new SqlParameter("@Unknow1", SqlDbType.NVarChar ,50),
new SqlParameter("@Unknow2", SqlDbType.NVarChar ,50),
new SqlParameter("@Unknow3", SqlDbType.NVarChar ,50),
new SqlParameter("@Favorite", SqlDbType.Int)};
            parameters[0].Value = model.Symbol;
            parameters[1].Value = model.Type;
            parameters[2].Value = model.OneHand;
            parameters[3].Value = model.Name;
            parameters[4].Value = model.PointIndex;
            parameters[5].Value = model.YestClose;
            parameters[6].Value = model.Unknow1;
            parameters[7].Value = model.Unknow2;
            parameters[8].Value = model.Unknow3;
            parameters[9].Value = model.Favorite;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            return rows;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static bool Delete(string symbol)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbl_Stock_Info ");
            strSql.Append(" where Symbol=@symbol");
            SqlParameter[] parameters = {
                    new SqlParameter("@symbol", SqlDbType.VarChar ,10)
            };
            parameters[0].Value = symbol;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public static int DeleteList(string symbollist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbl_Stock_Info ");
            strSql.Append(" where Symbol in (" + symbollist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            return rows;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static StockInfo GetModel(string symbol)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Symbol, Type, OneHand, Name, PointIndex, YestClose, Unknow1, Unknow2, Unknow3, Favorite from tbl_Stock_Info ");
            strSql.Append(" where Symbol=@symbol");
            SqlParameter[] parameters = {
                    new SqlParameter("@symbol", SqlDbType.VarChar ,10)
            };
            parameters[0].Value = symbol;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return ModelConvertHelper<StockInfo>.ConvertToModel(ds.Tables[0])?[0];
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Symbol, Type, OneHand, Name, PointIndex, YestClose, Unknow1, Unknow2, Unknow3, Favorite ");
            strSql.Append(" FROM tbl_Stock_Info ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public static DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Symbol, Type, OneHand, Name, PointIndex, YestClose, Unknow1, Unknow2, Unknow3, Favorite ");
            strSql.Append(" FROM tbl_Stock_Info ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public static int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tbl_Stock_Info ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.Symbol desc");
            }
            strSql.Append(")AS Row, T.*  from tbl_Stock_Info T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static List<StockInfo> GetStockCodeList(string strWhere)
        {
            DataSet ds = GetList("Unknow2<>0 and Name not like '%指数%' ");
            return ModelConvertHelper<StockInfo>.ConvertToModel(ds.Tables[0]).ToList();
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "tbl_Stock_Info";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/
    }
}